package com.ruoyi.energy.utils;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.Map;


public class HMACSHA256 {
    /**
     * 检测sign
     *
     * @param map
     * @return
     */
    public static boolean checkSignUtil(Map<String, String[]> map) {
        String t_s = "";
        String[] arr = {};
        for (Map.Entry<String, String[]> entry : map.entrySet()) {
            t_s = entry.getKey();
            arr = entry.getValue();
        }
        System.out.println("checkSignUtil的t_s=" + t_s);
        String[] ts = t_s.split("_");
        String t = ts[0];
        String key = "" + t.charAt(0) + t.charAt(2) + t.charAt(4) + t.charAt(6) + t.charAt(8) + t.charAt(9);
        System.out.println("checkSignUtil的我生成的key=" + key);
        String hash = sha256_HMAC(arr, key);
        System.out.println("checkSignUtil的我生成的sign=" + hash);
        return hash.equals(ts[1]);
    }

    /**
     * 获取sign
     *
     * @param t
     * @return
     */
    public static String getSignUtil(String t, String[] arr) {
        String key = "" + t.charAt(0) + t.charAt(2) + t.charAt(4) + t.charAt(6) + t.charAt(8) + t.charAt(9);
        String hash = sha256_HMAC(arr, key);
        return hash;
    }


    /**
     * sha256_HMAC加密
     *
     * @param arr    消息
     * @param secret 秘钥
     * @return 加密后字符串
     */
    private static String sha256_HMAC(String[] arr, String secret) {
        String hash = "";
        try {
            Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
            SecretKeySpec secret_key = new SecretKeySpec(secret.getBytes("UTF-8"), "HmacSHA256");
            sha256_HMAC.init(secret_key);
            //数组排序后穿串
            String message = px(arr);
            byte[] bytes = sha256_HMAC.doFinal(message.getBytes("UTF-8"));
            hash = byteArrayToHexString(bytes);
        } catch (Exception e) {
        }
        return hash;
    }

    //数组排序
    public static String px(String[] str) {
        String result = "";
        Arrays.sort(str);
        for (String string : str) {
            result += string;
        }
        return result;
    }

    /**
     * 将加密后的字节数组转换成字符串
     *
     * @param b 字节数组
     * @return 字符串
     */
    private static String byteArrayToHexString(byte[] b) {
        StringBuilder hs = new StringBuilder();
        String stmp;
        for (int n = 0; b != null && n < b.length; n++) {
            stmp = Integer.toHexString(b[n] & 0XFF);
            if (stmp.length() == 1)
                hs.append('0');
            hs.append(stmp);
        }
        return hs.toString().toLowerCase();
    }


    /**
     * 数据data组装
     *
     * @param json
     * @return
     */
    public static String getTaskCreateJsonReq(String json) {
        StringBuffer req = new StringBuffer();
        try {
            String encode = URLEncoder.encode(json, "UTF-8");
            req.append("?data=" + encode);
        } catch (UnsupportedEncodingException e) {
            req.append("?data=");
            System.out.println(e);
        }
        return req.toString();
    }
}
